package thread;

import org.junit.Test;
import util.ThreadUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author wuguojian
 * @description
 * @date 2022/7/9 下午8:40
 **/
public class ExecutorTest {

    @Test
    public void scheduledTest() {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
        // 循环任务，按照上一次任务的发起时间计算下一次任务的开始时间
        for (int i = 0; i < 2; i++) {
            scheduler.scheduleAtFixedRate(() -> {
                ThreadUtil.print("开始");
                ThreadUtil.sleep(3000);
                ThreadUtil.print("结束");
                if (1 == 1) {
                    throw new RuntimeException("异常");
                }
                ThreadUtil.print("异常");
            }, 0, 1, TimeUnit.SECONDS);
            ThreadUtil.sleep(2000);
            ThreadUtil.print("主线程");
        }
    }

}
